package parsers;

import java.util.regex.Pattern;

/**
 * 
 * @author aprokofjev
 *
 */
public class Z7LogParser extends BaseLogFileParser {

    private static final String DATE_FORMAT = "yyyy-MM-dd HH:mm:ss.SSS";

    private static final Pattern ENTRY_BEGIN_PATTERN = Pattern
            .compile("^\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}\\.\\d{3}.*$");

    @Override
    protected boolean  addLine(String line) throws Exception {
        if (ENTRY_BEGIN_PATTERN.matcher(line).matches()) {
            // Record previous found entry if exists, then create a new one
            prepareNewEntry();
            fillEntry(line);
            return true;
        }
       return false;
    }

    public String getMessage(String line) {
        String str = line.substring(line.indexOf(']') + 1);
        return str.substring(str.indexOf(']') + 1);
    }

    public String getLogLevel(String line) {
        String str = line.substring(line.indexOf(' ') + 1);
        return str.substring(str.indexOf(' ') + 1, str.indexOf('['));
    }

    public String getEmitter(String line) {
        String str = line.substring(line.indexOf(']') + 1);
        return str.substring(str.indexOf('[') + 1, str.indexOf(']'));
    }

    public String getPID(String line) {
        return line.substring(line.indexOf('[') + 1, line.indexOf(']'));
    }

    public String getDateFromLine(String line) {
        return line.substring(0, line.indexOf('.') + 4);
    }

    @Override
    public String getDateFormat() {
        return DATE_FORMAT;
    }

    @Override
    public String getParserName() {
        return "Z7log parser";
    }

    @Override
    public String getSupportedFileType() {
        return "LogMX Z7 log files";
    }
}
